//
//  AFMenuItemCell.m
//  AFChat
//
//  Created by Void on 2018/6/25.
//  Copyright © 2018年 afei. All rights reserved.
//

#import "AFMenuItemCell.h"

#define     WIDTH_ICON_RIGHT        31
#define     EGDE_SUB_TITLE          8

@implementation AFMenuItemCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleGray;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [self p_initSubviews];
    }
    return self;
}

- (void)setMenuItem:(AFMenuItem *)menuItem {
    _menuItem = menuItem;
    
    // icon
    if (menuItem.iconURL) { // 优先展示网络配置的图片
        [self.iconView tt_setImageWithURL:menuItem.iconURL.toURL placeholderImage:[UIImage imageNamed:menuItem.iconName]];
    } else if (menuItem.iconName) {
        [self.iconView setImage:[UIImage imageNamed:menuItem.iconName]];
    }
    
    //标题
    [self.titleLabel setText:menuItem.title];
    
    // 气泡
    [self.badgeView setHidden:YES];
    if (menuItem.badge) {
        [self.badgeView setHidden:NO];
        [self.badgeView setBadgeValue:menuItem.badge];
        [self.badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(menuItem.badgeSize);
        }];
    } else {
        [self.badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(0);
        }];
    }
    
    // 右侧说明
    [self.detailLabel setText:menuItem.subTitle];
    
    // 右侧图片
    [self.rightBadgeView setHidden:YES];
    if (menuItem.rightIconURL.length > 0) {
        [self.rightImageView setHidden:NO];
        [self.rightImageView tt_setImageWithURL:menuItem.rightIconURL.toURL];
        if (menuItem.subTitle.length > 0) {
            [self.detailLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-WIDTH_ICON_RIGHT - EGDE_SUB_TITLE);
            }];
        }
        // 图片上方气泡
        if (menuItem.showRightIconBadge) {
            [self.rightBadgeView setHidden:NO];
        }
    } else {
        [self.rightImageView setHidden:YES];
        if (menuItem.subTitle.length > 0) {
            [self.detailLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.contentView.mas_right).mas_offset(0);
            }];
        }
    }
}

#pragma mark - # Private Methods
- (void)p_initSubviews {
    
    // icon
    self.iconView = [UIImageView new];
    [self.contentView addSubview:self.iconView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15.0f);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(25.0f);
    }];
    
    // 标题
    self.titleLabel = [UILabel new];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(self.iconView.mas_right).mas_offset(15.0f);
        make.right.mas_lessThanOrEqualTo(-15.0f);
    }];
    
    // 气泡
    [self.contentView addSubview:self.badgeView];
    [self.badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel.mas_right).mas_offset(15.0f);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(18.0f);
    }];
    
    // 右图
    [self.contentView addSubview:self.rightImageView];
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView.mas_right).mas_offset(0);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(WIDTH_ICON_RIGHT);
    }];
    
    // 描述
    [self.contentView addSubview:self.detailLabel];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.greaterThanOrEqualTo(self.badgeView.mas_right).mas_offset(15.0f);
        make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-WIDTH_ICON_RIGHT - EGDE_SUB_TITLE);
        make.centerY.mas_equalTo(self.iconView);
    }];
    
    // 设置优先级
    [self.detailLabel setContentCompressionResistancePriority:500 forAxis:UILayoutConstraintAxisHorizontal];
    [self.titleLabel setContentCompressionResistancePriority:600 forAxis:UILayoutConstraintAxisHorizontal];
    
}
#pragma mark - # Getters
- (TLBadge *)badgeView
{
    if (!_badgeView) {
        _badgeView = [[TLBadge alloc] initWithFrame:CGRectMake(0, 0, 0, 18)];
    }
    return _badgeView;
}

- (TLBadge *)rightBadgeView
{
    if (!_rightBadgeView) {
        _rightBadgeView = [[TLBadge alloc] initWithFrame:CGRectMake(0, 0, 0, 18)];
        [_rightBadgeView setBadgeValue:@""];
    }
    return _rightBadgeView;
}

- (UIImageView *)rightImageView
{
    if (_rightImageView == nil) {
        _rightImageView = [[UIImageView alloc] init];
        
        [_rightImageView addSubview:self.rightBadgeView];
        [self.rightBadgeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_rightImageView.mas_right);
            make.centerY.mas_equalTo(_rightImageView.mas_top).mas_offset(1);
            make.size.mas_equalTo(8);
        }];
        
    }
    return _rightImageView;
}

- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:14.0f];
        _detailLabel.textColor = [UIColor grayColor];
    }
    return _detailLabel;
}


@end
